unit WaveFormConversion;

interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, VCLTee.TeEngine, VCLTee.TeeProcs, VCLTee.Chart, VCLTee.Series;

(**** TrgbRec  will hold pixel data for 24bit BMPs ****)
type
  TrgbRec = record
    R: Byte;
    G: Byte;
    B: Byte;
end;

procedure convertBitmap(fileName: string; bm: TBitMap; var outputList: TStringList);

implementation

(*******************************************************************************
 convertBitmap(): Takes a bitmap parameter, copies the bitmap in a rotated format
                  (90 degrees right) into a an array, and then locates the highest
                  color value in each column (by evaluating each pixel, scanning
                  each column from bottom to top). The x offset of the highest
                  pixel is then stored in a tstringlist. This output data becomes
                  the data to be used to drive the arbitrary waveform generator.
 ******************************************************************************)
procedure convertBitmap(fileName: string; bm: TBitMap; var outputList: TStringList);
var
  x, y, row, col, tmp : Integer;
  colorSum, colorMaxSum, xOffset : Integer;
  rgbArray: array of TrgbRec;
  pRGB: ^TrgbRec;
  rotationArray: array of array of TrgbRec;
begin
  //BitMap := TBitMap.create;
  outputList := TStringList.Create;
  try
    SetLength(rgbArray, bm.Width*bm.Height);
    SetLength(rotationArray, bm.Width+1, bm.Height+1);
    pRGB := @rgbArray;
    for y := 0 to bm.Height - 1 do
      begin
        pRGB := bm.ScanLine[y];
        for x := 0 to bm.Width - 1 do
          begin
            rotationArray[x, bm.Height-y] := pRGB^;
            inc(pRGB);
           end;  //end for x
      end;    //end for y
    for row  := 0 to bm.Width-1 do      //R is row
      begin
        colorMaxSum := 0;
        xOffset := 0;
        for col := 0 to bm.Height-1  do      //col is column
          begin
            colorSum := RotationArray[row, col].R + RotationArray[row, col].G +
                        RotationArray[row, col].B;
            if colorSum > colorMaxSum then
              begin
                colorMaxSum := colorSum;
                xOffset := col;
              end;
          end;
        outputList.Add(IntToStr(xOffset));
      end;
  except
    ShowMessage('Unable to process bitmap.');
  end;
end;



end.
